package com.whf.converter;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.whf.dataobject.OrderDetail;
import com.whf.dto.OrderDTO;
import com.whf.enums.ResultEnum;
import com.whf.exception.SellException;
import com.whf.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * Created by Administrator on 2018/7/15 0015.
 */
@Slf4j
public class OrderForm2OrderDTO {
    public static OrderDTO convert(OrderForm orderForm){
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        Gson gson=new Gson();
        List<OrderDetail> orderDetailList  = null;
        try {
            orderDetailList = gson.fromJson(orderForm.getItems(), new TypeToken<List<OrderDetail>>() {
            }.getType());
        } catch (JsonSyntaxException e) {
            log.error("[参数转换]:json参数转换错误,orderItems={}",orderForm.getItems());
            throw new SellException(ResultEnum.ORDER_ERROR_PARAMS);
        }
        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }
}
